package com.simonf.dynamic_programing;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: smionf
 * @Date: 2024/02/19/20:13
 * @Description:
 */
public class LC509FibonacciSequence {

    public static void main(String[] args) {
        System.out.println(fib(4));

    }

    public static int fib(int n) {
//F(n) = F(n - 1) + F(n - 2)
        if(n==0)
            return 0;
        if(n==1)
            return 1;

        /**
        * @Description:
         *n=2,f(2)=f(1)+f(0)=1,result=0+1,f(b)=
        */
        long fa =1 ,fb = 0,result = 0;
        for(long i=2;i<=n;i++){
            result = fa + fb;
            fb = fa;
            fa = result;
        }
        return (int)fa;

    }

}
